<?php

class Admin extends Controller
{
	
	function __construct()
	{
		parent::Controller();
		$this->quickauth->restrict('admin');
		$this->load->model('user_admin_model');
	}
	
	function Admin()
	{
		self::__construct();
	}
	
	function index()
	{	
		$pages['user'] = $this->user_admin_model->list_users();
		
		foreach($pages['user'] as $var => $value)
		{
			$username = $value['username'];
			$pages['group'][$username] = $this->user_admin_model->get_group($value['group_id']);
		}
		
		$data = array('body_id' => 'pages');
		
		$this->load->view('header', $data);
		$this->load->view('manage_users', $pages);
		$this->load->view('footer');
	}
	
	function add()
	{
		$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
		
		if($this->form_validation->run('add_user_admin'))
		{
			
			$password = sha1($this->config->item('encryption_key').$this->input->post('user_password'));
			
			$userdata = array(
								'username' => $this->input->post('user_username'),
                                'display_name' => $this->input->post('display_name'),
								'password' => $password,
								'email' => $this->input->post('user_email'),
								'group_id' => $this->input->post('user_group')
							);
			
			$this->db->insert('users', $userdata);
			
			$recent = array(
							'username' => $_SESSION['username'],
							'module' => 'user',
							'action' => 'Added user '.$this->input->post('user_username')
							);
							
			$this->db->insert('recent', $recent);
			
			
			$data = array('body_id' => 'pages');
			
			$data2 = array(
							'title' => 'User Added',
							'message' => 'Congratulations, the user has now been added.'
						);
			
			$this->load->view('header', $data);
			$this->load->view('message', $data2);
			$this->load->view('footer');
		} // if
		else
		{
			$id = $this->uri->segment('4');
			$groups['group'] = $this->user_admin_model->list_groups();
			
			$data = array('body_id' => 'pages');

			$this->load->view('header', $data);
			$this->load->view('add_user', $groups);
			$this->load->view('footer');
		} // else
	} // add
	
	function edit()
	{
		
		$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
		
		if($this->form_validation->run('edit_user_admin'))
		{
			
			$id = $this->uri->segment('4');
			$user = $this->user_admin_model->get_user($id);
			$password = $this->quickauth->encrypt($this->input->post('user_password'));
			
			$userdata = array(
								'email' => $this->input->post('user_email'),								
                                'display_name' => $this->input->post('display_name'),
								'group_id' => $this->input->post('user_group')
							);
			if($this->input->post('user_password') !== NULL or $this->input->post('user_password') !== "")
            {
                //array_merge($userdata, array('password' => $password));
				$userdata['password'] = $password;
            }
            
			$this->db->where('username', $user['username']);
			$this->db->update('users', $userdata);
			
			$recent = array(
							'username' => $_SESSION['username'],
							'module' => 'user',
							'action' => 'Updated user '.$user['username']
							);
							
			$this->db->insert('recent', $recent);
			
			$data = array('body_id' => 'pages');
			
			$data2 = array(
							'title' => 'User Updated',
							'message' => 'Congratulations, the user has now been updated.'
						);

			$this->load->view('header', $data);
			$this->load->view('message', $data2);
			$this->load->view('footer');
		} // if
		else
		{
			$id = $this->uri->segment('4');
			$user_data['user'] = $this->user_admin_model->get_user($id);
			$user_data['group'] = $this->user_admin_model->list_groups();

			$data = array('body_id' => 'user');

			$this->load->view('header', $data);
			$this->load->view('edit_user', $user_data);
			$this->load->view('footer');
		} // else
	} // edit
	
	function delete()
	{
		$id = $this->uri->segment('4');
		
		$user = $this->user_admin_model->get_user();
		
		$this->db->delete('users', array('id' => $id));
		
		$recent = array(
						'username' => $_SESSION['username'],
						'module' => 'user',
						'action' => 'Deleted user '.$user['username']
						);
						
		$this->db->insert('recent', $recent);
		
		$data = array('body_id' => 'users');
		
		$data2 = array(
						'title' => 'User Deleted',
						'message' => 'Congratulations, the user has now been deleted.'
					);
		
		$this->load->view('header', $data);
		$this->load->view('message', $data2);
		$this->load->view('footer');
		
	} // delete
	
	function activate()
	{
		$id = $this->uri->segment('4');
		
		$user = $this->user_admin_model->get_user();
		
		$data = array(
					   'activated' => 1
					);
		
		$this->db->where('id', $id);
		$this->db->update('users', $data); 
		
		$recent = array(
						'username' => $_SESSION['username'],
						'module' => 'user',
						'action' => 'Activated user '.$user['username']
						);
						
		$this->db->insert('recent', $recent);
		
		$data = array('body_id' => 'users');
		
		$data2 = array(
						'title' => 'User Activated',
						'message' => 'Congratulations, the user has now been activated.'
					);
		
		$this->load->view('header', $data);
		$this->load->view('message', $data2);
		$this->load->view('footer');
		
	} // activate

	function deactivate()
	{
		$id = $this->uri->segment('4');

		$user = $this->user_admin_model->get_user();

		$data = array(
					   'activated' => 0
					);

		$this->db->where('id', $id);
		$this->db->update('users', $data);

		$recent = array(
						'username' => $_SESSION['username'],
						'module' => 'user',
						'action' => 'Deactivated user '.$user['username']
						);

		$this->db->insert('recent', $recent);

		$data = array('body_id' => 'users');

		$data2 = array(
						'title' => 'User Deactivated',
						'message' => 'This user has now been deactivated.'
					);

		$this->load->view('header', $data);
		$this->load->view('message', $data2);
		$this->load->view('footer');

	} // activate
	
	function username_exists($str)
	{
		$this->db->where('username', $str);
		$query = $this->db->get('users');
		
		if($query->num_rows() > 0)
		{
			$this->form_validation->set_message('username_exists', 'The username already exists');
			return FALSE;
		}
		
		return TRUE;
	}
	
} // admin

/* End of file admin.php */
/* Location: modules/user/controllers/admin.php */